#!/usr/bin/env python3
"""
Script: run.py
Function: Run both restricted stopping power and ionization simulations
          in the background (parallel execution)
Author: Converted from bash script
"""

import os
import subprocess
import sys
import threading


def run_script(script_name):
    """Run a Python script in the current directory"""
    try:
        subprocess.run([sys.executable, script_name], check=True)
    except subprocess.CalledProcessError as e:
        print(f"Error running {script_name}: {e}")
    except FileNotFoundError:
        print(f"Script {script_name} not found")


def main():
    # Check if required scripts exist
    if not os.path.exists("run_rspw.py"):
        print("Error: run_rspw.py not found!")
        sys.exit(1)

    if not os.path.exists("run_ionization.py"):
        print("Error: run_ionization.py not found!")
        sys.exit(1)

    print("Starting both simulations in parallel...")

    # Create threads for parallel execution
    rspw_thread = threading.Thread(target=run_script, args=("run_rspw.py",))
    ionization_thread = threading.Thread(target=run_script, args=("run_ionization.py",))

    # Start both threads
    rspw_thread.start()
    ionization_thread.start()

    # Wait for both to complete
    rspw_thread.join()
    ionization_thread.join()

    print("Both simulations completed!")


if __name__ == "__main__":
    main()
